﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;

namespace TenpointSystem.Common.Db
{
    /// <summary>
    /// mysql连接类
    /// 来控制mysql的连接，关闭，事务的开启，提交及回滚
    /// </summary>
    class MySqlConnector
    {
        /// <summary>
        /// 链接字符串
        /// </summary>
        private static string thisConnectionString = string.Empty;
        /// <summary>
        /// 连接
        /// </summary>
        private static MySqlConnection connection = null;
        /// <summary>
        /// 是否在事务内
        /// </summary>
        internal static bool isTransaction = false;
        /// <summary>
        /// 事务
        /// </summary>
        private static MySqlTransaction transaction = null;

        /// <summary>
        /// 初始化链接
        /// </summary>
        /// <param name="connectionString">连接字符串</param>
        public static void initConnection(String connectionString)
        {
            thisConnectionString = connectionString;
            connection = new MySqlConnection(thisConnectionString);
        }

        public static MySqlConnection getConnection()
        {
            return connection;
        }
        /// <summary>
        /// 打开数据库
        /// </summary>
        public static void openDataAccess()
        {
            connection.Open();
        }
        /// <summary>
        /// 关闭数据库
        /// </summary>
        public static void closeDataAccess()
        {
            connection.Close();
        }
        /// <summary>
        /// 开启事务
        /// </summary>
        public static void transactionStart()
        {
            transaction = connection.BeginTransaction();
            isTransaction = true;
        }
        /// <summary>
        /// 事务提交
        /// </summary>
        public static void transactionCommit()
        {
            transaction.Commit();
            isTransaction = false;
        }
        /// <summary>
        /// 事务回滚
        /// </summary>
        public static void transactionRollback()
        {
            transaction.Rollback();
            isTransaction = false;
        }


    }
}
